package com.xx.executor.core.thread;


import com.xx.common.pojo.XxlJobInfo;
import com.xx.executor.core.cron.CronExpression;
import com.xx.executor.core.scheduler.ScheduleTypeEnum;

import java.util.Date;

/**
 * @author xuxueli 2019-05-21
 */
public class JobScheduleHelper {

    public static final long PRE_READ_MS = 5000;

    // ---------------------- tools ----------------------
    public static Date generateNextValidTime(XxlJobInfo jobInfo, Date fromTime) throws Exception {
        ScheduleTypeEnum scheduleTypeEnum = ScheduleTypeEnum.match(jobInfo.getScheduleType(), null);
        if (ScheduleTypeEnum.CRON == scheduleTypeEnum) {
            Date nextValidTime = new CronExpression(jobInfo.getScheduleConf()).getNextValidTimeAfter(fromTime);
            return nextValidTime;
        } else if (ScheduleTypeEnum.FIX_RATE == scheduleTypeEnum /*|| ScheduleTypeEnum.FIX_DELAY == scheduleTypeEnum*/) {
            return new Date(fromTime.getTime() + Integer.valueOf(jobInfo.getScheduleConf()) * 1000);
        }
        return null;
    }

}
